home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / DEV / C-H / CHexDmpDA.cpt / Hex Dump DA / find.HexDumpDA.c < prev    next >
C/C++ Source or Header  |  1988-05-26  |  5KB  |  269 lines

  1. #include <DialogMgr.h>
  2. #include <ControlMgr.h>
  3. #include <FileMgr.h>
  4. #include "HexDump.h"
  5.  
  6. #define FINDBUFSIZE 2048
  7.  
  8. Handle                 findBufH;
  9. long                fBegin;
  10.  
  11. static DialogPtr     dp;
  12. static Str255        findString;
  13. static Str255        displayString = "";
  14. static long            fBufFirst;
  15.  
  16. static struct find_record {
  17.     enum { Hex, ASCII } findBy;
  18.     Boolean        fromBeginning;
  19.     Boolean        ignoreCase;
  20. } fParms = { ASCII, true, false };
  21.  
  22. extern long            sBegin;
  23. extern int            fileRef;
  24. extern long            fileSize;
  25. extern Cursor        wait;
  26.  
  27. doFind()
  28. {
  29.     if (Conduct_Find_Dialog()) {
  30.         doUpdate();
  31.         fBegin = -1;
  32.         fBufFirst = -1;
  33.         doFindAgain();
  34.     }
  35. }
  36.  
  37. doFindAgain()
  38. {
  39.     long    delta, new, max;
  40.     GrafPtr    savePort;
  41.     
  42.     SetCursor(&wait);
  43.     if (find(findString)) {
  44.         New_Select(fBegin, fBegin + findString[0]);
  45.         Show_Point(fBegin);
  46.     }
  47.     else SysBeep(10);
  48.     InitCursor();
  49. }
  50.  
  51. /* Find Dialog Item Numbers: */
  52. #define OK            1
  53. #define CANCEL         2
  54. #define FINDSTRING    3
  55. #define FINDHEX        4
  56. #define FINDASCII    5
  57. #define FROMBEGIN    7
  58. #define IGNORECASE    8
  59.  
  60. Init_Find()
  61. {
  62.     findBufH = NewHandle(FINDBUFSIZE);
  63.     if (!findBufH) {
  64.         ErrorStr("\pNot enough memory for search.");
  65.         return 0;
  66.     }
  67.     return 1;
  68. }
  69.  
  70. Conduct_Find_Dialog()
  71. {
  72.     int item;
  73.     Str255 s;
  74.     Handle stringH;
  75.     Rect box;
  76.     int type;
  77.     struct find_record fNewParms;
  78.     
  79.     fNewParms = fParms;
  80.     dp = GetNewDialog( OwnedResourceID(FIND_DIALOG), 0L, -1L );    
  81.     Setup_Dialog();
  82.     do {
  83.         for (;;) {
  84.             ModalDialog( 0L, &item );
  85.             switch (item) {
  86.                 case CANCEL:
  87.                     DisposDialog(dp);
  88.                     return 0;
  89.                 case FINDASCII:
  90.                     fNewParms.findBy = ASCII;
  91.                     Set_Buttons(&fNewParms);
  92.                     break;
  93.                 case FINDHEX:
  94.                     fNewParms.findBy = Hex;
  95.                     Set_Buttons(&fNewParms);
  96.                     break;
  97.                 case FROMBEGIN:
  98.                     fNewParms.fromBeginning = !fNewParms.fromBeginning;
  99.                     Set_Buttons(&fNewParms);
  100.                     break;
  101.                 case IGNORECASE:
  102.                     fNewParms.ignoreCase = !fNewParms.ignoreCase;
  103.                     Set_Buttons(&fNewParms);
  104.                     break;
  105.                 default: ;
  106.             }
  107.             if (item==OK) break;
  108.         }
  109.         GetDItem( dp, FINDSTRING, &type, &stringH, &box );
  110.         GetIText( stringH, s );
  111.     } while (!valid(s, &fNewParms));
  112.  
  113.     pstrcpy( displayString, s );
  114.     fParms = fNewParms;
  115.     if (fParms.findBy==ASCII) {
  116.         pstrcpy( findString, displayString );
  117.         if (fParms.ignoreCase) to_lower( &findString[1], (int)findString[0] );
  118.     }
  119.     else Hex_to_String( findString, displayString );
  120.  
  121.     DisposDialog(dp);
  122.     return 1;
  123. }
  124.  
  125. Set_Buttons(f)
  126. struct find_record *f;
  127. {
  128.     int type;
  129.     Rect box;
  130.     
  131.     Handle Aitem, Hitem, Bitem, Citem;
  132.     
  133.     GetDItem(dp, FINDASCII, &type, &Aitem, &box);
  134.     GetDItem(dp, FINDHEX, &type, &Hitem, &box);
  135.     GetDItem(dp, FROMBEGIN, &type, &Bitem, &box);
  136.     GetDItem(dp, IGNORECASE, &type, &Citem, &box);
  137.     
  138.     if (f->findBy==ASCII) {
  139.         SetCtlValue( (ControlHandle)Aitem, 1 );
  140.         SetCtlValue( (ControlHandle)Hitem, 0 );
  141.         HiliteControl( (ControlHandle)Citem, 0 );
  142.     }
  143.     else {
  144.         SetCtlValue( (ControlHandle)Aitem, 0 );
  145.         SetCtlValue( (ControlHandle)Hitem, 1 );
  146.         HiliteControl( (ControlHandle)Citem, 255 );
  147.     }
  148.     if (f->fromBeginning) 
  149.         SetCtlValue( (ControlHandle)Bitem, 1 );
  150.     else
  151.         SetCtlValue( (ControlHandle)Bitem, 0 );
  152.     if (f->ignoreCase) 
  153.         SetCtlValue( (ControlHandle)Citem, 1 );
  154.     else
  155.         SetCtlValue( (ControlHandle)Citem, 0 );
  156.         
  157. }
  158.  
  159. static
  160. Setup_Dialog()
  161. {
  162.     int     Stype;
  163.     Handle     Sitem;
  164.     Rect     Sbox;
  165.     
  166.     Set_Buttons(&fParms);
  167.     GetDItem(dp, FINDSTRING, &Stype, &Sitem, &Sbox);
  168.     SetIText(Sitem, displayString);
  169.     SelIText(dp, FINDSTRING, 0, displayString[0]);
  170. }
  171.  
  172. static
  173. valid(s, f)
  174. register char *s;
  175. struct find_record *f;
  176. {
  177.     register int i;
  178.  
  179.     /* if findBy is ASCII, everything is valid */
  180.     /* if findBy is hex, the only valid chars are 0..9, A..F, a..f */
  181.     if (f->findBy==ASCII) return 1;
  182.     for (i=*s++; i>0; --i, ++s) {
  183.         if ((*s>='0')&&(*s<='9')) continue;
  184.         if ((*s>='A')&&(*s<='F')) continue;
  185.         if ((*s>='a')&&(*s<='f')) continue;
  186.         ErrorStr("\pInvalid character in hex search string.");
  187.         return 0;
  188.     }
  189.     return 1;
  190. }
  191.  
  192. static 
  193. Fill_Buffer(filePos)
  194. long filePos;
  195. {
  196.     long count = FINDBUFSIZE;
  197.     
  198.     SetFPos( fileRef, fsFromStart, filePos );
  199.     FSRead( fileRef, &count, *findBufH );
  200.     if (fParms.ignoreCase) to_lower( *findBufH, (int)count );
  201. }
  202.  
  203. static
  204. find()
  205. {
  206.     long    start;
  207.     long     found;
  208.     
  209.     if (!findString[0]) {
  210.         ErrorStr("\pThere is no search string!");
  211.         return 0;
  212.     }
  213.     if (fBufFirst<0) {            /* find... */
  214.         if (fParms.fromBeginning)
  215.             Fill_Buffer(fBufFirst = 0L);
  216.         else 
  217.             Fill_Buffer(fBufFirst = sBegin+1);
  218.         start = 0;
  219.     }
  220.     else if ((sBegin+1<fBufFirst) || 
  221.              ((sBegin+findString[0])>=(fBufFirst+FINDBUFSIZE)) ) {
  222.         Fill_Buffer(fBufFirst = sBegin+1);
  223.         start = 0;
  224.     }
  225.     else start = sBegin + 1 - fBufFirst;
  226.  
  227.     do {
  228.         if ( (found = Munger( findBufH, start,
  229.                     &findString[1], (long)findString[0], 0L, 0L))>=0 ) {
  230.             fBegin = found+fBufFirst;
  231.             return 1;
  232.         }
  233.         fBufFirst += FINDBUFSIZE - findString[0];
  234.         if (fBufFirst<fileSize) 
  235.             Fill_Buffer(fBufFirst);
  236.     } while (fBufFirst<fileSize);
  237.     return 0;
  238. }
  239.  
  240. hexval(c)
  241. {
  242.     if ((c>='0')&&(c<='9')) return c-'0';
  243.     if ((c>='A')&&(c<='F')) return c-'A'+10;
  244.     if ((c>='a')&&(c<='f')) return c-'a'+10;
  245. }
  246.     
  247. Hex_to_String( s1, s2 )
  248. register char *s1, *s2;
  249. {
  250.     register int len;
  251.     *s1++ = (*s2+1)/2;
  252.     if ((len = *s2++)%2) {
  253.         *s1++ = hexval(*s2++);
  254.         len -= 1;
  255.     }
  256.     while (len) {
  257.         *s1 = 16*hexval(*s2++);
  258.         *s1++ += hexval(*s2++);
  259.         len -= 2;
  260.     }
  261. }
  262.  
  263. to_lower(s,n)
  264. register char *s;
  265. register int    n;
  266. {
  267.     for (; n>0; --n,++s)
  268.         if ((*s>='A') && (*s<='Z')) *s -= 'A'-'a';
  269. }